package com.abvnet.project.water.project.controller;

import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.framework.aspectj.lang.annotation.Log;
import com.abvnet.framework.aspectj.lang.enums.BusinessType;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.framework.web.page.TableDataInfo;
import com.abvnet.project.water.project.domain.ProjectCase;
import com.abvnet.project.water.project.service.IProjectCaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 案例详情信息操作处理
 * 
 * @author duj
 * @date 2019-08-26
 */
@Controller
@RequestMapping("/project/projectCase")
@Slf4j
public class ProjectCaseController extends BaseController {
    private String prefix = "project/projectCase";
	
	@Autowired
	private IProjectCaseService projectCaseService;
	
	@RequiresPermissions("project:projectCase:view")
	@GetMapping()
	public String projectCase()
	{
	    return prefix + "/list";
	}
	
	/**
	 * 查询案例详情列表
	 */
	@RequiresPermissions("project:projectCase:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ProjectCase projectCase) {
		startPage();
        List<ProjectCase> list = projectCaseService.selectProjectCaseList(projectCase);
		return getDataTable(list);
	}
	
	/**
	 * 新增案例详情
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存案例详情
	 */
	@RequiresPermissions("project:projectCase:add")
	@Log(title = "案例详情", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ProjectCase projectCase) {
		return toAjax(projectCaseService.insertProjectCase(projectCase));
	}

	/**
	 * 修改案例详情
	 */
	@GetMapping("/edit/{caseId}")
	public String edit(@PathVariable("caseId") Long caseId, ModelMap mmap) {
		ProjectCase projectCase = projectCaseService.selectProjectCaseById(caseId);
		mmap.put("projectCase", projectCase);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存案例详情
	 */
	@RequiresPermissions("project:projectCase:edit")
	@Log(title = "案例详情", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ProjectCase projectCase,@RequestParam("caseFile") MultipartFile caseFile) {
        try {
            if (caseFile != null && !caseFile.isEmpty()) {
                String caseUrl = FileUploadUtils.upload(AbvnetConfig.getCasePath(), caseFile);
                projectCase.setCaseUrl(caseUrl);
            }
            return toAjax(projectCaseService.updateProjectCase(projectCase));
        } catch (Exception e) {
            log.error("更新案例详情失败！", e);
            return error(e.getMessage());
        }
	}
	
	/**
	 * 删除案例详情
	 */
	@RequiresPermissions("project:projectCase:remove")
	@Log(title = "案例详情", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(projectCaseService.deleteProjectCaseByIds(ids));
	}
	
}
